import time

t1 = time.time()
# -*- coding:utf-8 -*
# python 实现N个数组的排列组合(笛卡尔积算法)
class Cartesian():
    # 初始化
    def __init__(self, datagroup):
        self.datagroup = datagroup
        # 二维数组从后往前下标值
        self.counterIndex = len(datagroup)-1
        # 每次输出数组数值的下标值数组(初始化为0)
        self.counter = [0 for i in range(0, len(self.datagroup))]
        self.res = []
    # 计算数组长度

    def countlength(self) -> object:
        i = 0
        length = 1
        while i < len(self.datagroup):
            length *= len(self.datagroup[i])
            i += 1
        return length

    # 递归处理输出下标
    def handle(self):
        # 定位输出下标数组开始从最后一位递增
        self.counter[self.counterIndex]+=1
        # 判断定位数组最后一位是否超过长度，超过长度，第一次最后一位已遍历结束
        if self.counter[self.counterIndex] >= len(self.datagroup[self.counterIndex]):

            # 重置末位下标
            self.counter[self.counterIndex] = 0
            # 标记counter中前一位
            self.counterIndex -= 1
            # 当标记位大于等于0，递归调用
            if self.counterIndex >= 0:
                self.handle()
            # 重置标记
            self.counterIndex = len(self.datagroup)-1

    # 排列组合输出
    def assemble(self):
        length = self.countlength()
        i = 0
        while i < length:
            attrlist1 = []
            j = 0
            while(j<len(self.datagroup)):
                attrlist1.append(self.datagroup[j][self.counter[j]])
                j += 1
            self.res.append(attrlist1)
            self.handle()
            i += 1


def duplicates(num_list: object) -> object:
    '''
    判断一个列表中是否有重复的元素
    直接使用set方法
    '''
    if len(num_list)!=len(set(num_list)):
        return True
    else:
        return False


def func3(n):
    return n * (n + 1) // 2


def func4(n):
    return n ** 2


def func5(n):
    '''n(3n−1)/2'''
    return n * (3 * n - 1) // 2


def func6(n):
    return n * (2 * n - 1)


def func7(n):
    return n * (5 * n - 3) // 2


def func8(n):
    return n * (3 * n - 2)


def getSeqData(func=func3):
    i = 1
    _num = []
    n = 0
    while n < 10000:
        n = int(func(i))
        if 1000 <= n < 10000:
            if str(n)[2] != '0':
                _num.append(n)
        i += 1
    return _num


def is_all_type(types):
    '''判断是否有符合的类型是全部的类型'''
    com = list(range(3,9))
    res = False
    for i in types:
        a = sorted(i)
        if a==com:
            res = True
            return res
    return res


all_nums = {}
comd_ = 'all_nums[#] = getSeqData(func=func#)'
for i in range(3, 9):
    exec_cmd = comd_.replace('#', str(i))
    exec(exec_cmd)

all_type = all_nums.keys()

aset = []
for k, v in all_nums.items():
    aset.extend(v)

# 获取每个数的所属类型，注意这里一个数可能会对应多种类型
all_app = {}
distinct_aset = set(aset)
for a in distinct_aset:
    for k, v in all_nums.items():
        if a in v:
            all_app.setdefault(a, [])
            all_app[a].append(k)


def getSeqDataType(now_seq, all_app=all_app):
    datagroup1=[]
    for seq_num in now_seq:
        now_types = all_app[seq_num].copy()
        datagroup1.append(now_types)
    cartesian = Cartesian(datagroup1)
    cartesian.assemble()
    res = cartesian.res.copy()
    return [i for i in res if not duplicates(i)]


def getSeqDataType2(now_seq, all_app=all_app):
    datagroup1=[]
    for seq_num in now_seq:
        now_types = all_app[seq_num].copy()
        datagroup1.append(now_types)
    cartesian = Cartesian(datagroup1)
    cartesian.assemble()
    res = cartesian.res.copy()
    return [i for i in res if not duplicates(i)]


def getSeqDataType1(now_seq, all_app=all_app):
    datagroup1=[]
    for seq_num in now_seq:
        datagroup1.extend(all_app[seq_num])
    b = set(datagroup1)
    a = list(b)
    return a


# 获取每个数的后拼接数
all_can_concat = {}
for i in distinct_aset:
    if str(i)[2:] == str(i)[:2]:
        all_can_concat.setdefault(i, [i])
    else:
        all_can_concat.setdefault(i, [])
    for v in distinct_aset:
        if v != i and str(i)[2:] == str(v)[:2]:
            all_can_concat[i].append(v)

def fun_main():
    b = list(range(3,9))
    ress = []
    for i1 in distinct_aset:
        ress = [i1]
        for i2 in all_can_concat[i1]:
            if len(ress) >= 2:
                ress = ress[:1]
            ress.append(i2)
            for i3 in all_can_concat[i2]:
                if len(ress) >= 3:
                    ress = ress[:2]
                ress.append(i3)
                for i4 in all_can_concat[i3]:
                    if len(ress) >= 4:
                        ress = ress[:3]
                    ress.append(i4)
                    for i5 in all_can_concat[i4]:
                        if len(ress) >= 5:
                            ress = ress[:4]
                        ress.append(i5)
                        i6 = int(str(i5)[2:]+str(i1)[:2])
                        if i6 in all_can_concat[i5]:
                            ress.append(i6)
                            types = getSeqDataType2(ress)
                            for ty in types:
                                tm = sorted(ty)
                                if tm == b:
                                    print(sum(ress))
                                    return sum(ress)


fun_main()

print(time.time()-t1)